package es.ceura.wilder.logger;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 *
 * @author Jorge Madrid
 */
public enum Logger {

    INSTANCE;
    private static final String logNameTemplate = "log_{{date}}.txt";
    private static final String logPath = "logs/";
    private String logName;
    private Calendar calendar;
    private SimpleDateFormat dateFormat, dateFormatWrite;
    private File logFile;
    private BufferedWriter writer = null;
    private FileWriter fileWriter = null;

    private Logger() {
        try {
            calendar = Calendar.getInstance();
            dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
            dateFormatWrite = new SimpleDateFormat("yyyy MM dd - HH:mm:ss");
            createLogFile();

            initLogger();

            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {
                        log("Shuting down logger");
                        closeLogger();
                    } catch (Exception ignore) {
                    }
                }
            });
        } catch (IOException exIgnore) {
            //FATAL ERROR
        }
    }

    private void createLogFile() throws IOException {
        logName = logNameTemplate.replace("{{date}}", getDate(dateFormat));
        logFile = new File(logPath + logName);
        logFile.createNewFile();
    }

    private String getDate(SimpleDateFormat formater) {
        return formater.format(calendar.getTime());
    }

    public void log(String message) {
        try {
            writer.newLine();
            writer.newLine();

            writer.write("Log Date-> " + getDate(dateFormatWrite));
            writer.newLine();
            writer.write("Log Message -> ");

            writer.write(message);

        } catch (IOException ex) {
        } finally {
            try {
                writer.flush();
            } catch (IOException ex) {
            }
        }
    }

    public void logError(String message, Throwable ex) {
        try {
            writer.newLine();
            writer.newLine();

            writer.write("Log Date-> " + getDate(dateFormatWrite));
            writer.newLine();
            writer.write("Log ERROR Message -> ");

            writer.write(message);

            if (ex != null) {
                writer.newLine();
                writer.write("Exception -> ");
                writer.write(ex.getMessage());
            }

        } catch (IOException ignore) {
        } finally {
            try {
                writer.flush();
            } catch (IOException ignore) {
            }
        }
    }

    public void logError(String message) {
        this.logError(message, null);
    }

    private void initLogger() throws IOException {
        fileWriter = new FileWriter(logFile);
        writer = new BufferedWriter(fileWriter);
        log("Starting Logger system...");
    }

    public void closeLogger() {
        try {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        } catch (IOException ex) {
        }
        try {
            if (fileWriter != null) {
                fileWriter.close();
            }
        } catch (IOException ex) {
        }
    }
}
